package com.hq.fxh.No2_BBS.Fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ListView;

import com.hq.fxh.No2_BBS.Activity.Plate_DetialActivity;
import com.hq.fxh.No2_BBS.Activity.PlateSelectActivity;
import com.hq.fxh.No2_BBS.Adapter.Ladapter_PlateSort;
import com.hq.fxh.Modle.PlateSoreModle;
import com.hq.fxh.R;
import com.hq.fxh.Receiver.ListCallBack;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.HttpApi.UserServiceImpl;

import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * /**
 *
 * @author 薛
 * @ClassName
 * @Description : 论坛板块内容页
 * @Created 2017/8/18
 * @Email xlwenhao1858@sina.com
 */


public class BBS_Plate_Fragment extends Fragment implements AppBarLayout.OnOffsetChangedListener {
    protected View rootView;
    protected ListView listPlateSortName;
    protected FrameLayout fragmentPlate;
    private FragmentManager fragmentManager;
    HashMap<Integer, Plate_Name_Fragment> fragments = new HashMap<>();
    Ladapter_PlateSort ladapter_plateSort;

    int status = 1;  // 1 正常跳转页面    2 帖子编辑选择页面

    public static BBS_Plate_Fragment newInstance() {
        Bundle args = new Bundle();
        BBS_Plate_Fragment fragment = new BBS_Plate_Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    PlateSelectActivity activity;

    public void fetchData() {

        LogUtils.e("帖子分类数据");
        UserServiceImpl.getPlateSore(new Callback<PlateSoreModle>() {
            @Override
            public void onResponse(Call<PlateSoreModle> call, final Response<PlateSoreModle> response) {
                final List<PlateSoreModle.DataBean> data = response.body().getData();

                if (ladapter_plateSort == null) {
                    ladapter_plateSort = new Ladapter_PlateSort(new ListCallBack() {
                        @Override
                        public void onCall(View view, int position, Object itemdata) {
                            addFragment(data, position);
                            if (status == 2) {
                                activity = (PlateSelectActivity) getActivity();

                                PlateSoreModle.DataBean itemdata1 = (PlateSoreModle.DataBean) itemdata;
                                activity.setFirstSort(itemdata1.getId(), itemdata1.getCategory());
                            }

                        }
                    });
                    if (data == null || data.size() == 0) {

                    } else {
                        ladapter_plateSort.setData(data);
                        ladapter_plateSort.notifyDataSetChanged();

                        if (status == 2) {
                            activity = (PlateSelectActivity) getActivity();
                            PlateSoreModle.DataBean dataBean = data.get(0);
                            activity.setFirstSort(dataBean.getId(), dataBean.getCategory());

                         }
                    }
                    listPlateSortName.setAdapter(ladapter_plateSort);
                    addFragment(data, 0);

                } else {
                    if (data == null || data.size() == 0) {

                    } else {
                        ladapter_plateSort.setData(data);
                        ladapter_plateSort.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(Call<PlateSoreModle> call, Throwable t) {

            }
        });

    }

    private void addFragment(List<PlateSoreModle.DataBean> plateSoreModle, int position) {
        Plate_Name_Fragment fragment = fragments.get(position);
        if (fragment == null) {
            fragment = new Plate_Name_Fragment();
            List<PlateSoreModle.DataBean.ItemsBean> items = plateSoreModle.get(position).getItems();

            fragment.setData(items, plateSoreModle.get(position).getId());
            fragments.put(position, fragment);
        }
        if (status == 2) {
            fragment.setType(2);
        }
        switchContent(fragment, position);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_bbs_plate, null);
        initView(rootView);
        initData();

        fetchData();
        return rootView;
    }

    private void initData() {
        fragmentManager = getChildFragmentManager();
    }

    private void initView(View rootView) {
        listPlateSortName = (ListView) rootView.findViewById(R.id.list_Plate_SortName);
        fragmentPlate = (FrameLayout) rootView.findViewById(R.id.fragment_Plate);
    }


    int ispage = -1;

    public void switchContent(Fragment to, int page) {
        if (page != ispage) {
            ispage = page;
            FragmentTransaction transaction = fragmentManager.beginTransaction();
            List<Fragment> fragments = fragmentManager.getFragments();

            if (!to.isAdded()) {    // 先判断是否被add过
                transaction.add(R.id.fragment_Plate, to); // 隐藏当前的fragment，add下一个到Activity中
                transaction.commitAllowingStateLoss();
                transaction = fragmentManager.beginTransaction();
                if (fragments == null) {
                    transaction.show(to).commitAllowingStateLoss();
                } else {
                    for (Fragment f : fragments) {
                        transaction.hide(f);
                    }
                    transaction.show(to).commitAllowingStateLoss();

                }
                return;
            }

            for (Fragment f : fragments) {
                transaction.hide(f);
            }
            transaction.show(to).commitAllowingStateLoss(); // 隐藏当前的fragment，显示下一个
        }

    }


    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        LogUtils.e("verticalOffset" + verticalOffset);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (Plate_DetialActivity.appBarLayout != null)
            Plate_DetialActivity.appBarLayout.addOnOffsetChangedListener(this);
    }
}
